package cn.clx.ymcc.config;

import cn.clx.ymcc.domain.Permission;
import cn.clx.ymcc.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

import java.util.List;

/**
 * Security 认证配置类
 */
@Configuration
@EnableWebSecurity //开启认证授权
@EnableGlobalMethodSecurity(prePostEnabled=true) //开启方法授权 !!!!  securedEnabled = true 不要写成这个了
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private IPermissionService permissionService;

     //提供用户信息，这里没有从数据库查询用户信息，在内存中模拟，这里直接注入了一个UserDetailsService 对象 写死了的
   /* @Bean
    public UserDetailsService userDetailsService(){
        InMemoryUserDetailsManager inMemoryUserDetailsManager =
        new InMemoryUserDetailsManager();
        inMemoryUserDetailsManager.createUser(User.withUsername("zs").password("123").authorities("admin").build());
        return inMemoryUserDetailsManager;
    }*/

    //配置认证管理器，授权模式为“poassword”时会用到，比对密码的时候要用到
    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }


    //密码编码器：不加密  NoOpPasswordEncoder.getInstance();
    //密码编译器 ： 加密
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
    
    //授权规则配置
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        /**
         * web方式实现授权
         * 查询权限表，给每个路径，设置需要什么样的权限
         * 没有在这个上面的路径，就是默认为静态资源，只要登录了就可以访问
         */
       /* List<Permission> permissions = permissionService.selectList(null);
        permissions.forEach(permission -> {
            try {
                http.authorizeRequests().antMatchers(permission.getResource()).hasAuthority(permission.getSn());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });*/
        http.authorizeRequests()                                //授权配置
                .antMatchers("/login/common","/login/refresh").permitAll()  //登录路径放行
                .anyRequest().authenticated()                   //其他路径都要认证之后才能访问
                .and().formLogin()                              //允许表单登录
                //.successForwardUrl("/loginSuccess")             // 设置登陆成功页
                .and().logout().permitAll()                    //登出路径放行 /logout
                .and().csrf().disable();                        //关闭跨域伪造检查
    }
}